package util
{
	
	/**
	 * A static class for generating differnent random numbers.
	 */
	public class Random
	{
		/**
		 * Generate a random Number between min and max.
		 * @param	min		the lower bounds. defaults to zero.
		 * @param	max		the upper bounds. defaults to one.
		 * @return
		 */
		public static function uniform(min:Number = 0.0, max:Number = 1.0):Number
		{
			return Math.random() * (max - min) + min;
		}
		
		/**
		 * Generate a random int n where n in range [min,max)
		 * @param	min		the lower bounds. Defaults to zero
		 * @param	max		the upper bounds. Defaults to one.
		 * @return
		 */
		public static function integer(min:Number = 0, max:Number = 1):int
		{
			return int(Math.floor(Math.random() * (max - min) + min));
		}
		
		/**
		 * Generate a random Number based on the normal distribution
		 * @param	mean	the mean of the distribution
		 * @param	sigma	the standard deviation of the distribution
		 * @return
		 */
		public static function normal(mean:Number = 0.0, sigma:Number = 1.0):Number
		{
			return (sigma * (-1.5 + Math.random() + Math.random() + Math.random())) + mean;
		}
		
		/**
		 * Returns either -1 or 1
		 * @return	-1 or 1
		 */
		public static function sign():int
		{
			return Math.random() > 0.5 ? -1 : 1;
		}
		
		/**
		 * Return either true or false.
		 * @return	true or false
		 */
		public static function bool():Boolean
		{
			return (Math.random() > 0.5);
		}
		
		/**
		 * Create a completely random color
		 * @return	color
		 */
		public static function color():int
		{
			return integer(0, 255) + integer(0, 255) * 0x100 + integer(0, 255) * 0x10000;
		}
		
		/**
		 * Create a color that deviates from another color by a random amount.
		 * @param	c	the color
		 * @param	r	red deviation
		 * @param	g	green deviation
		 * @param	b	blue deviation
		 * @return
		 */
		public static function colorDeviation(c:int, dr:int, dg:int, db:int):int
		{
			var r:int = c % 256;
			var g:int = (c / 256) % 256;
			var b:int = (c / 65536) % 256;
			
			r = integer(MyMath.max2(0, r - dr), MyMath.min2(255, r + dr));
			g = integer(MyMath.max2(0, g - dg), MyMath.min2(255, g + dg));
			b = integer(MyMath.max2(0, b - db), MyMath.min2(255, b + db));
			
			return r + g * 0x100 + b * 0x10000;
		}
		
		/**
		 * Choose an int from a list of ints.
		 * @param	list	list of ints to choose from
		 * @return	a random int from the list.
		 */
		public static function chooseInt(list:Vector.<int>):int
		{
			return list[integer(0, list.length)];
		}
		
		/**
		 * Choose an object from a list of objects.
		 * @param	list	list of objects to choose from
		 * @return	a random object from the list.
		 */
		public static function choose(list:Array):Object
		{
			return list[integer(0, list.length)];
		}
	
	}

}